<template>
  <div class="page pd-l" >
    <div class="pd-xl ta-c">
      <div style="width: 100px;margin: 0 auto;">
        <image src="https://ark-1256750454.cos.ap-shanghai.myqcloud.com/app-statics/wexin.png" mode="widthFix"></image>
      </div>
      <div class="pdt c-gray">请使用微信登录</div>
    </div>
    <div class="pd-xxl">
      <button v-if="isLogin" class="ui btn primary" open-type="getUserInfo" @getuserinfo="onGetUserInfo">确认登录</button>
      <button v-else class="ui btn primary disabled" >确认登录</button>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import querystring from 'querystring'

export default {
  data () {
    return {
      isLogin: false
    }
  },
  components: {
  },
  async onShow () {
    try {
      let flag = await this.login()
      this.isLogin = flag
    } catch (e) {
      this.$debug({err: e})
    }
  },
  methods: {
    ...mapActions([
      'getUserInfo',
      'login'
    ]),
    getJumpPageUrl () {
      let pages = getCurrentPages()
      if (pages[pages.length - 2]) {
        let page = pages[pages.length - 2]
        let queryString = ''
        if (page.options) {
          queryString = querystring.stringify(page.options)
        }
        let pageUrl = '/' + page.route
        if (queryString) {
          pageUrl += '?' + queryString
        }
        return pageUrl
      }
      return '/page/index/main'
    },
    async onGetUserInfo ({mp: { detail: {encryptedData, iv} }}) {
      await this.getUserInfo({encryptedData, iv})
      // 进行跳转...
      let jumpUrl = this.getJumpPageUrl()
      wx.navigateBack()
    }
  }
}
</script>

<style scoped>
</style>
